package spittr.config;

import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration;

/**
 * 配置前端控制器、设置DispatcherServerlet加载上下文的配置文件和ContextLoaderListener加载上下文的配置文件
 * 不知道这个类要怎么写代码才能启动SpringMVC
 */
public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    /**
     * 创建ContextLoaderListener
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{RootConfig.class};
    }

    /**
     * 创建DispatcherServerlet
     * @return
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    /**
     * 配置上传文件保存位置
     * @param registration
     */
    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        registration.setMultipartConfig(new MultipartConfigElement("C:\\Users\\fsk\\Desktop\\springinaction\\spring-in-action-test\\springMVC\\src\\main\\resources\\spittr\\uploads"));
    }

    /**
     * 自定义Filter，书上例子给的不详细、没有MyFilter的实现
     * @return
     */
    @Override
    protected Filter[] getServletFilters() {
//        return new Filter[]{new MyFilter()};
        return null;
    }
}
